package com.lxmes.gs.controller;

import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import com.lxmes.gs.domain.costListVO.*;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.lxmes.gs.domain.CostList;
import com.lxmes.gs.service.ICostListService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 成本清单Controller
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Api(tags = "成本清单")    
@RestController
@RequestMapping("/gs/costList")
public class CostListController extends BaseController{
    @Autowired
    private ICostListService costListService;

    /**
     * 查询成本清单列表
     */
    @ApiOperation("查询成本清单列表")
    @PreAuthorize("@ss.hasPermi('gs:costList:list')")
    @GetMapping("/list")
    public TableDataInfo list(CostListSearchVO costList){
        startPage();
        List<CostList> list = costListService.selectCostListList(BeanUtil.copyProperties(costList,CostList.class));
        return getDataTable(Convert.convert(new TypeReference<List<CostListResultVO>>() {},list));
}

    /**
     * 导出成本清单列表
     */
    @ApiOperation("导出成本清单列表")
    @PreAuthorize("@ss.hasPermi('gs:costList:export')")
    @Log(title = "成本清单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CostList costList){
        List<CostList> list = costListService.selectCostListList(costList);
        List<CostListExportVO> exportList = Convert.convert(new TypeReference<List<CostListExportVO>>() {}, list);
        ExcelUtil<CostListExportVO> util = new ExcelUtil<CostListExportVO>(CostListExportVO.class);
        util.exportExcel(response, exportList, "成本清单数据");
    }

    /**
     * 获取成本清单详细信息
     */
    @ApiOperation("获取成本清单详细信息")
    @PreAuthorize("@ss.hasPermi('gs:costList:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(costListService.selectCostListById(id));
    }

    /**
     * 新增成本清单
     */
    @ApiOperation("新增成本清单")
    @PreAuthorize("@ss.hasPermi('gs:costList:add')")
    @Log(title = "成本清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CostListCreateVO costList){
        return toAjax(costListService.insertCostList(BeanUtil.copyProperties(costList,CostList.class)));
    }

    /**
     * 修改成本清单
     */
    @ApiOperation("修改成本清单")
    @PreAuthorize("@ss.hasPermi('gs:costList:edit')")
    @Log(title = "成本清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CostListUpdateVO costList){
        return toAjax(costListService.updateCostList(BeanUtil.copyProperties(costList,CostList.class)));
    }

    /**
     * 删除成本清单
     */
    @ApiOperation("删除成本清单")
    @PreAuthorize("@ss.hasPermi('gs:costList:remove')")
    @Log(title = "成本清单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(costListService.deleteCostListByIds(ids));
    }
    /**
     * 成本清单审核
     */
    @ApiOperation("成本清单审核")
    @PreAuthorize("@ss.hasPermi('gs:costList:audit')")
    @Log(title = "成本清单审核")
	@PostMapping("/auditCostList/{ids}")
    public AjaxResult auditCostList(@PathVariable Long[] ids){
        return toAjax(costListService.auditCostList(ids));
    }
    /**
     * 导入成本清单
     */
    @Log(title = "成本清单", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<CostListImportVO> util = new ExcelUtil<>(CostListImportVO.class);
        List<CostListImportVO> costListList = util.importExcel(file.getInputStream());
        String message = costListService.imporIteminfo(Convert.convert(new TypeReference<List<CostList>>() {},costListList), updateSupport);
        return AjaxResult.success(message);
    }
    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException{
        ExcelUtil<CostListImportVO> util = new ExcelUtil<>(CostListImportVO.class);
        util.importTemplateExcel(response,"成本清单");
    }
}
